#ifndef __ARRAY_H
#define __ARRAY_H
#include "stdtype.h"

/**
 * get the length of array
 * @prototype: mx_usize_t mx_array_len(array);
 * @note: make sure that the array symbol is in context
 */
#define mx_array_len(array) (sizeof(array) / sizeof((array)[0]))

/**
 * iterate the specified array
 * @prototype: void mx_array_foreach (mx_carray_pt, <node>, {...});
 * @note: make sure that the array symbol is in context
 */
#define mx_array_foreach(array, elm, ...) { \
    mx_size_t len = mx_array_len(array);    \
    for (mx_typeof((array)[0])* elm = &(array)[0]; elm < &(array)[len]; elm++) { \
        __VA_ARGS__ \
    }   \
}

/**
 * iterate the specified array
 * @prototype: void mx_array_foreach (mx_carray_pt, <node>, index, {...});
 * @note: make sure that the array symbol is in context
 */
#define mx_array_foreachi(array, elm, i, ...) { \
    mx_size_t len = mx_array_len(array);    \
    mx_size_t i = 0;    \
    for (mx_typeof((array)[0])* elm = &(array)[0]; elm < &(array)[len]; elm = &(array)[++i]) { \
        __VA_ARGS__ \
    }   \
}

#endif
